# Copyright 2024 Huawei Technologies Co., Ltd
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# ============================================================================
"""
Llava Base Model
"""
from mindformers.mindformer_book import MindFormerBook
from mindformers.models.base_model import BaseModel
from llava_config import LlavaConfig
from llava_llama import LlamaForLlava
from llava_adapter import LlavaAdapter
from llava_clip_vit import VisionTransformer


class LlavaBase(BaseModel):
    """
    Llava BaseModel, all llava models inherit this class.
    """
    # _support_list = MindFormerBook.get_model_support_list()['llava']

    def init_adapter(self):
        """
        Init adapter for llava model
        """
        if isinstance(self.config, LlavaConfig):
            adapter = LlavaAdapter(self.config)
        else:
            raise ValueError(f"For llm model, the config should be LlavaConfig, but got {type(self.config)}")
        return adapter

    def init_vision_encoder(self):
        """
        init vision encoder for llava model
        """
        if isinstance(self.config, LlavaConfig):
            visual_encoder = VisionTransformer(self.config)
        else:
            raise ValueError(f"For llm model, the config should be LlavaConfig, but got {type(self.config)}")
        return visual_encoder

    def init_llm(self):
        """"
        init llm model for llava model
        """
        if isinstance(self.config, LlavaConfig):
            llm_model = LlamaForLlava(self.config)
        else:
            raise ValueError(f"For llm model, the config should be LlavaConfig, but got {type(self.config)}")
        return llm_model
